﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using ReadyApps.Common;
using ReadyApps.Common.Exceptions;
using ReadyApps.Data;
using ReadyApps.Data.Wiki;
using ReadyApps.Wiki.Templates;

namespace ReadyApps.Wiki.Views
{
    class RevisionView : ContentViewBase, IViewFiresTitleSetEvent
    {
        #region Variables and Constants

        private int revisionNumber;

        #endregion

        #region Events

        private event EventHandler viewRevisionsButtonClick;
        public event EventHandler ViewRevisionsButtonClick
        {
            add { viewRevisionsButtonClick += value; }
            remove { viewRevisionsButtonClick -= value; }
        }

        private event EventHandler cloneToHeadButtonClick;
        public event EventHandler CloneToHeadButtonClick
        {
            add { cloneToHeadButtonClick += value; }
            remove { cloneToHeadButtonClick -= value; }
        }

        #endregion

        #region Constructor

        public RevisionView(int wikiPageID, int wikiRevisionNumber, WikiControlSettings wikiControlSettings,
            IWikiDataProvider wikiDataProvider, IWikiUrlManager wikiUrlManager)
            : base(wikiPageID, wikiControlSettings, wikiDataProvider, wikiUrlManager)
        {
            revisionNumber = wikiRevisionNumber;
        }

        #endregion

        #region Public Methods

        public override void BuildControlTree(ControlCollection controls, bool raiseEvents)
        {
            Control container = new Control();

            // Instantiate the template
            ITemplate layoutTemplate = controlSettings.LayoutTemplate ?? new WikiDefaultLayoutTemplate();
            layoutTemplate.InstantiateIn(container);

            // Locate the content and wiki controls control
            Control contentControl = WebUtilities.FindControlRecursive(container, WikiController.CONTENT_CONTROL_ID);
            if (contentControl == null)
            {
                throw new MissingControlException(WikiController.CONTENT_CONTROL_ID, location: "layout template");
            }
            Control controlsControl = WebUtilities.FindControlRecursive(container, WikiController.CONTROLS_CONTROL_ID);
            if (controlsControl == null)
            {
                throw new MissingControlException(WikiController.CONTROLS_CONTROL_ID, location: "layout template");
            }

            // Lookup page info
            PageContent content = dataProvider.GetPageContentForRevision(pageID, revisionNumber);

            // Fill the page content
            GetContentControls(content, contentControl);

            WikiControlButton manageRevisions = WikiControlButton.GetFromTemplate(controlSettings.ButtonTemplate);
            manageRevisions.ButtonControl.Text = "Revisions";
            manageRevisions.ButtonControl.Click += viewRevisionsButtonClick;
            controlsControl.Controls.Add(manageRevisions.RootControl);

            controlsControl.Controls.Add(new Literal() { Text = WikiController.BUTTON_SPACER });

            WikiControlButton cloneToHead = WikiControlButton.GetFromTemplate(controlSettings.ButtonTemplate);
            cloneToHead.ButtonControl.Text = "Make Current";
            cloneToHead.ButtonControl.Click += cloneToHeadButtonClick;
            controlsControl.Controls.Add(cloneToHead.RootControl);

            controlsControl.Controls.Add(new Literal() { Text = "<br />" });
            Label pendingLabel = new Label();
            pendingLabel.Text = "This is a past revision";
            pendingLabel.ForeColor = System.Drawing.Color.Firebrick;
            pendingLabel.Font.Bold = true;
            controlsControl.Controls.Add(pendingLabel);

            controls.Clear();
            controls.Add(container);

            if (raiseEvents)
            {
                controlSettings.OnTitleSet(this, new TitleSetEventArgs(content.Title));
            }
        }

        #endregion
    }
}
